﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{
    public partial class frmAddReference : Form
    {
        public int UserId = 0;

        public int referenceId = 0;

        public frmAddReference()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateReference())
            {
                LDMSRepository repository = new LDMSRepository();
                ReferenceBook refBook = new ReferenceBook();
                refBook.CaseID = Convert.ToInt32(cmbCase.SelectedValue);
                refBook.CreatedBy = UserId;
                refBook.CreatedDate = DateTime.Now;
                refBook.Notes = txtNotes.Text.Trim();
                refBook.RefDate = dtRefDate.Value;
                refBook.Tags = txtTags.Text.Trim();
                repository.AddReference(refBook);
                MessageBox.Show("Reference has been saved successfully", Resources.MessageCaption);
                ResetControl();
            }
        }

        private void ResetControl()
        {
            referenceId = 0;
            txtNotes.Text = string.Empty;
            dtRefDate.Value = DateTime.Now;
            txtTags.Text = string.Empty;
        }

        private bool ValidateReference()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtTags.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireTag, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtNotes.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireNotes, Resources.MessageCaption);
                isValid = false;
            }
            return isValid;
        }

        private void frmAddReference_Load(object sender, EventArgs e)
        {
            LDMSRepository repository = new LDMSRepository();
            var cases = repository.GetAllCases();
            cmbCase.DataSource = cases;
            cmbCase.DisplayMember = "CaseTitle";
            cmbCase.ValueMember = "CaseID";

            cmbSearchCase.DataSource = cases;
            cmbCase.DisplayMember = "CaseTitle";
            cmbCase.ValueMember = "CaseID";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
            if (!string.IsNullOrEmpty(txtTag.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.TAG, txtTag.Text));
            }
            if (cmbSearchCase.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.CASE, Convert.ToString(cmbSearchCase.SelectedValue)));
            }
            if (dtpReferenceDate.Checked)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.REFDATE, Convert.ToString(dtpReferenceDate.Value)));
            }
            if (parameters.Count > 0)
            {
                LDMSRepository repository = new LDMSRepository();
                var reference = repository.SearchReference(parameters);
                dgReference.DataSource = reference;
            }
            else
            {
                MessageBox.Show(Resources.NoneFilter, Resources.MessageCaption);
            }
        }

        private void dgReference_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dgReference.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals("Edit"))
            {
                txtTags.Text = Convert.ToString(dgReference.Rows[e.RowIndex].Cells["Tags"].Value);

                txtNotes.Text = Convert.ToString(dgReference.Rows[e.RowIndex].Cells["Notes"].Value);

                dtRefDate.Value = Convert.ToDateTime(dgReference.Rows[e.RowIndex].Cells["RefDate"].Value);

                cmbCase.SelectedIndex = cmbCase.Items.IndexOf(cmbCase.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgReference.Rows[e.RowIndex].Cells["CaseID"].Value)));

                referenceId = Convert.ToInt32(dgReference.Rows[e.RowIndex].Cells["RefID"].Value);

                dgReference.DataSource = null;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
